<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Group extends CI_Controller {    
    private $content='';
    private $mensenger='';    
    
    private $total_rows = 100;
    private $per_page =20;
    
    function  __construct(){
        parent::__construct();  
        $this->load->library('form_validation');      
    }
    function index(){
        if($this->session->userdata('g_logedin')){
            redirect('group/account');
        }else{
            redirect('group/login');
        }
    }
    function acountmem($id=0){
        $log = $this ->Home_model->get_one('users',array('id'=>(int)$id));
        if(!empty($log)){
            $this->session->set_userdata('logedin',1);
            $this->session->set_userdata('user',$log); 
            redirect(base_url().'user/account');
        }
        
    }
    function subgroup($type='',$id=0,$offset=0){
        if($this->session->userdata('g_group')->idgroup==0){        
        
            if($type=='group'){
                $data=$this->Home_model->get_data('users_group',array('idgroup' =>$this->session->userdata('g_group')->id));
                $point =$this->get_credit($data);
                $this->content = $this->load->view('group/subgroup',
                        array(
                            'dulieu'=>$data,
                            'credit'=>$point
                        ),true
                    ); 
            }
            if($type=='listacc'){//check xem thah vien dang xem co dung cua nhom k
                if($this->Home_model->get_number('users_group',array('id'=>$id,'idgroup'=>$this->session->userdata('g_group')->id))){
                    $this->content = $this->account_subg($id,$offset);
                }else{
                    $this->content = 'Bạn không thể xem thành viên của group khác';
                }            
               
            }
            if($type=='edit'){//check xem thah vien dang xem co dung cua nhom k
            $dulieu = $this->Home_model->get_one('users_group',array('id'=>$id,'idgroup'=>$this->session->userdata('g_group')->id));
                if(!empty($dulieu)){
                    if($_POST){
                        $data=$this->security->xss_clean($_POST);
                        $data['password']=sha1(md5($data['password']));
                        $this->db->where('id',$id);
                        $this->db->update('users_group',$data);
                        redirect('group/subgroup/group');
                    }
                    $this->content = $this->load->view('group/edit_usergroup',array('dulieu'=>$dulieu),true);
                }else{
                    $this->content = 'Bạn không thể sửa thành viên của group khác';
                }
            }
            if($type=='add'){
               if($_POST){
                        $data=$this->security->xss_clean($_POST);
                        $data['type']=2;
                        $data['idgroup']=$this->session->userdata('g_group')->id;
                        if(!empty($data['password'])){
                            $data['password']=sha1(md5($data['password']));
                        }                    
                        $this->db->insert('users_group',$data);
                        redirect('group/subgroup/group');
                    }
               $this->content = $this->load->view('group/add_usergroup',array(),true); 
            }
            if($type=='delete'){//check xem thah vien dang xem co dung cua nhom k
            $dulieu = $this->Home_model->get_one('users_group',array('id'=>$id,'idgroup'=>$this->session->userdata('g_group')->id));
                if(!empty($dulieu)){
                    $this->db->where('id',$id);
                    $this->db->delete('users_group');
                    redirect('group/subgroup/group');                
                }else{
                    $this->content = 'Bạn không thể Xóa thành viên của group khác';
                }
            }
            if($type=='pay'){//check xem thah vien dang xem co dung cua nhom k
            $dulieu = $this->Home_model->get_one('users_group',array('id'=>$id,'idgroup'=>$this->session->userdata('g_group')->id));
                if(!empty($dulieu)){
                    //get point ghi vao bang history nua
                    $p = $this->credit_subgroup(array($id));
                    if(!empty($p->pending)){
                        $this->db->insert('history',array('iduser'=>$id,'point'=>$p->pending,'status'=>1,'ip'=>$this->input->ip_address() ));
                    }
                    $this->db->where(array('cashout'=>2,'groupid'=>$id));//cashout=2 admin da check qua neu k thi admi se k thay
                    $this->db->update('users',array('pending'=>0,'cashout'=>3,'show'=>0)); //sau khi cash thì đông thời ẩn luôn user               
                    redirect('group/subgroup/group');                
                }else{
                    $this->content = 'Bạn không thể pay thành viên của group khác';
                }
            }        
            $dta->content = $this->content;
        $dt = $this->template->render($dta);  
        $this->load->view('default/viewfull',$dt);
         }   
    }
    function changepass(){
        if($_POST){
            $this->form_validation->set_rules('oldpassword', 'Password cũ', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|min_length[5]|max_length[25]|xss_clean');
            $this->form_validation->set_rules('passconf', 'Confirm password', 'trim|required|xss_clean');            
            if ($this->form_validation->run() == TRUE){
                $data = $this->security->xss_clean($_POST);// du lieu gui ve tu form
                $password=sha1(md5($data['oldpassword']));
                if($this->Home_model->get_number('users_group',array('id'=>$this->session->userdata('g_group')->id,'password'=>$password))==1){
                    $this->db->where('id',$this->session->userdata('g_group')->id);
                    $this->db->update('users_group',array('password'=>sha1(md5($data['password']))));
                    $this->session->set_userdata('doipass',1);
                }else{
                    $this->session->set_userdata('loidoipass','Password không đúng vui lòng nhập lại');
                }
                    
                }                     
            }
           $this->content = $this->load->view('group/change_pass',array(),true);
           $this->hienthi();
    }
    function account_subg($id=0,$offset=0){//id la idgroup-- show =1 hien khi user remove acc cho show=0
        if(!is_numeric($id)){$id=0;}
        if(!is_numeric($offset)){$offset=0;}
        $this->db->where('show',1);
        $this->total_rows = $this->Home_model->get_number('users',array('groupid' =>$id,'show'=>1));
        $this->phantrang();
        $this->db->where('show',1);
        return $this->load->view('group/listacc',array(
                            'dulieu'=>$this->Home_model->get_data('users',array('groupid' =>$id,'show'=>1),array($this->per_page,$offset),array('id','DESC')),
                            'us'=>$this->Home_model->get_one('users_group',array('id' =>$id))
        )
        ,true
        );
    }
    
    
    //// get credit cua griup va user
    function get_credit($data=''){
        if(!empty($data)){            
            $where=$this->session->userdata('where');            
            $mdt=array();
            foreach($data as $data){//data la danh sach subgroup
                $point_idg[$data->id]=$this->credit_subgroup($data->id);
                $mdt[]=$data->id;// ghep cac id cus sub vao 1 mang de lay tong cho group
            }
            $point_idg['group']=$this->credit_subgroup($mdt);// cong tong cho group
            return $point_idg;//tra ve mang id cua subgroup tinh point cua tung sub            
        }        
    } 
    function credit_subgroup($array_id=''){
        $this->db->where('show',1);
        $this->db->where_in('groupid',$array_id);
        $this->db->select_sum('total');
        $this->db->select_sum('pending');
        $this->db->select_sum('curent');
        return $this->db->get('users')->row();
    }   
    ////////////////////////   
    function account_subgroup($b=0,$id=0,$offset=0){        
            $data->content = $this->account_subg($this->session->userdata('g_group')->id,$offset);
          $dt = $this->template->render($data);  
          $this->load->view('default/viewfull',$dt);
       
    }  
    function account(){
        if($this->session->userdata('g_logedin')){//check dang nhap
        if($this->session->userdata('g_group')->idgroup==0){
            $p=$this->get_credit($this->Home_model->get_data('users_group',array('idgroup'=>$this->session->userdata('g_group')->id)));        
        }else{
            $p=$this->credit_subgroup($this->session->userdata('g_group')->id);
           
        }
         
             $this->content=$this->load->view('group/account',
                    array(
                    'point'=>$p
                    )
                    ,true
                    );
            $this->hienthi(); 
            }else{
                redirect('group/login');
            }      
        
    }
    function payout()
    {
        if($this->session->userdata('g_group')->type==0){
            $this->session->set_userdata('g_thongbao','Bạn đã yêu cầu thanh toán, hoặc admin chưa cho phép thanh toán, xin hãy thực hiện lại sau');
            redirect('group/account');
        }else{//update type ve 0 va chuyen toan bo account cash out
            $this->db->where('id',$this->session->userdata('g_group')->id);
            $this->db->update('users_group',array('type'=>0));
            // chuyen curent sang pending
            $this->db->where(array('groupid'=>$this->session->userdata('g_group')->id,'cashout'=>0,'show'=>1));
            $this->db->set('pending', 'curent', FALSE);
            $this->db->set('curent', '0', FALSE);
            $this->db->set('cashout', '1');
            $this->db->update('users');
            
            $g = $this->Home_model->get_one('users_group',array('id'=>$this->session->userdata('g_group')->id));
            $this->session->set_userdata('g_group',$g);//update lai session vi truong type thay doi
            $this->session->set_userdata('g_thongbao','Gửi yêu cầu thanh toán thành công!');
            redirect('group/account');
        }
        
    }
    function history(){//xem lai luoc su thanh toan lay tu bang history (neu la user khong thuoc group thi lay tu pay)
         // khi admin pay cho group thi se add vao bang history 
         if($this->session->userdata('g_logedin')){           
            $this->content =$this->load->view('group/history',array(
                                    'pay'=>$this->Home_model->get_data('history',array('iduser' => $this->session->userdata('g_group')->id))  
                                    ),
            true);  
            $this->hienthi();         
        }else {
            $this->page = 'history';
            $this->content = 'Not login';
            $this->hienthi(); 
        }
        
    }
    function credit($id=0){// xem lai credit         
            // wall ->1 \\\ banner-> 2            
            $this->content =$this->load->view('group/history',
                array(
                'pay'=>$this->Home_model->get_data('credit',array('iduser' => $id))
                ),true
            );
            $this->hienthi(); 
        
    }
    
    function login(){ 
       if(!$this->session->userdata('g_logedin')){
    	   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
           $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');      
           if ($this->form_validation->run() == TRUE){
                $username = $this->security->xss_clean($_POST['username']);
                $password = $this->security->xss_clean($_POST['password']);
                $password = sha1(md5($password));
                $log = $this ->Home_model->get_one('users_group',array('username'=>$username,'password'=>$password));
                $num = $this ->Home_model->get_number('users_group',array('username'=>$username,'password'=>$password));
                //get_number            
                if($num==1){                
                   $this->session->set_userdata('g_logedin',1);
                   $this->session->set_userdata('g_group',$log); 
                   redirect('group/account');                                
                }else{                
                    $this->session->set_userdata('loidn','Incorrect login - <a href="'.base_url().'contact">Contact Admin</a>');
                }
           }
          $data->content = $this->load->view('group/login','',true);
          $dt = $this->template->render($data);  
          $this->load->view('default/viewfull',$dt);
        }else{
            redirect('group/account');
        }
	}
    
    function logout(){
        $this->session->unset_userdata('g_logedin');
        $this->session->unset_userdata('g_group'); 
        redirect(base_url());
    }
    function hienthi(){  
        $data->content = $this->content;
        $dt = $this->template->render($data);  
        $this->load->view('default/twocol',$dt);        
       
    }
    
    function phantrang(){
        $this->load->library('pagination');// da load ben tren
        $config['base_url'] = base_url().$this->uri->segment(1,0).'/'.$this->uri->segment(2,0).'/'.$this->uri->segment(3,0).'/'.$this->uri->segment(4,0).'/';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 5;
        $config['num_links'] = 10;
        $config['first_link'] = '<<';
        $config['first_tag_open'] = '<li class="firt_page">';//div cho chu <<
        $config['first_tag_close'] = '</li>';//div cho chu <<
        $config['last_link'] = '>>';
        $config['last_tag_open'] = '<li class="last_page">';
        $config['last_tag_close'] = '</li>';
        //-------next-
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        //------------preview
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
       // ------------------cu?npage
        $config['cur_tag_open'] = '<li class="current">';
        $config['cur_tag_close'] = '</li>';
        //--so 
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        //-----
        $this->pagination->initialize($config);
    
    }
    
}